const Error = require('./error')
const request = require('./request.js')
const storage = require('./storage.js')

const {
    isObject,
    isString,
    isNumber,
    isUndefined,
    isBoolean,
    isArray
} = require('./dataType.js')

const device = class device {

    constructor() {}

    get(id) {
        let params = {
            deviceId: id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.DEVICE.GET_DEVICE.url,
                MS._config.parameters.DEVICE.GET_DEVICE.method, params).then(res => {
                    resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    findDeviceByUserId(userId) {
        let params = {
            userId: userId
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.DEVICE.FIND_DEVICE_BY_USER_ID.url,
                MS._config.parameters.DEVICE.FIND_DEVICE_BY_USER_ID.method, params).then(res => {
                    resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    query(options) {
        let params = {
            pageNum: 1,
            pageSize: 10
        }
        if(!isUndefined(options)) {
            if(!isUndefined(options.searchCode) && isString(options.searchCode)) {
                params.searchCode = options.searchCode;
            }
            if(!isUndefined(options.userId) && isString(options.userId)) {
                params.userId = options.userId;
            }
            if(!isUndefined(options.sellerId) && isString(options.sellerId)) {
                params.sellerId = options.sellerId;
            }
            if(!isUndefined(options.pageNum) && isNumber(options.pageNum)) {
                params.pageNum = options.pageNum;
            }
            if(!isUndefined(options.pageSize) && isNumber(options.pageSize)) {
                params.pageSize = options.pageSize;
            }
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.DEVICE.SEARCH_DEVICE.url,
                MS._config.parameters.DEVICE.SEARCH_DEVICE.method, params).then(res => {
                    resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    remove(id) {
        let params = {
            deviceId: id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.DEVICE.REMOVE_DEVICE.url,
                MS._config.parameters.DEVICE.REMOVE_DEVICE.method, params).then(res => {
                    resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    save(options) {
        let params = {};
        if(!isUndefined(options)) {
            if(!isUndefined(options.id) && isString(options.id)) {
                params.id = options.id;
            }
            if(!isUndefined(options.userId) && isString(options.userId)) {
                params.userId = options.userId;
            }
            if(!isUndefined(options.name) && isString(options.name)) {
                params.name = options.name;
            }
            if(!isUndefined(options.sellerId) && isString(options.sellerId)) {
                params.sellerId = options.sellerId;
            }
            if(!isUndefined(options.sellerName) && isString(options.sellerName)) {
                params.sellerName = options.sellerName;
            }
            if(!isUndefined(options.brand) && isString(options.brand)) {
                params.brand = options.brand;
            }
            if(!isUndefined(options.picture) && isString(options.picture)) {
                params.picture = options.picture;
            }
            if(!isUndefined(options.model) && isString(options.model)) {
                params.model = options.model;
            }
            if(!isUndefined(options.synopsis) && isString(options.synopsis)) {
                params.synopsis = options.synopsis;
            }
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.DEVICE.SAVE_DEVICE.url,
                MS._config.parameters.DEVICE.SAVE_DEVICE.method, params).then(res =>{
                resolve(res.data)
            }).catch(err =>{
                reject(err)
            })
        })
    }

    divide(options) {
        let params = {}
        if(!isUndefined(options.userId) && isString(options.userId)) {
            params.userId = params.userId;
        }
        if(!isUndefined(options.deviceId) && isString(options.deviceId)) {
            params.deviceId = options.deviceId;
        }
        if(!isUndefined(options.mine) && isBoolean(options.mine)) {
            params.mine = options.mine;
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.DEVICE.DIVIDE_USER.url,
                MS._config.parameters.DEVICE.DIVIDE_USER.method, params).then(res => {
                    resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    detach(id) {
        let params = {
            id: id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.DEVICE.DETACH_USER.url,
                MS._config.parameters.DEVICE.DETACH_USER.method, params).then(res =>{
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }
}

module.exports = device